package com.kayak.fapp.transbase;

import java.math.BigDecimal;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kayak.exception.ErrorException;
import com.kayak.kkutils.check.IDCard;
import com.kayak.dbutil.dao.ComnDao;
import com.kayak.kkweb.sys.SysBeans;
public class TransUtil {

	protected final static Logger log = LoggerFactory.getLogger(TransUtil.class);
	protected static ComnDao comnDao = SysBeans.getComnDao();

	/**
	 * 获取交易流水号
	 * 
	 * @param h
	 * @return
	 * @throws ErrorException
	 */
	public static String getTransId() throws ErrorException {
		return comnDao.getSeqence("fapponl", "f1_trans_seq");
	}
	
	/**
	 * 获取序列号
	 * 
	 * @param h
	 * @return
	 * @throws Exception 
	 */
	public static String getSeqenceId() throws Exception {
		return comnDao.generateSeqence("ma_mgr_seq",6);
	}

	/**
	 * 移动：134、135、136、137、138、139、150、151、157(TD)、158、159、187、188 、178<br>
	 * 联通：130、131、132、152、155、156、185、186、176 <br>
	 * 电信：133、153、180、189、177、（1349卫通） 虚拟 : 170
	 */

	public final static String MOBILE_PATTERN = "^((13[0-9])|(15[0-9])|(18[0-9]|(17[0,6,7,8,9])))\\d{8}$";
	// 邮箱验证
	public final String EMAIL = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
	// 验证标识符必须由字母、数字、下划线组成
	public final String NUMBER = "^[1-9][0-9]\\d*$";
	/**
	 * 交易状态成功
	 */
	public final String TRANS_STATUS_SUCCESS = "0000";

	/**
	 * 交易状态失败
	 */
	public final String TRANS_STATUS_FAIL = "0999";

	/**
	 * 网络超时
	 * <p>
	 * 交易状态处理中
	 */
	public final String TRANS_STATUS_PROCESS = "3306";

	/**
	 * 账户类型
	 */
	public final String ACCT_TYPE = "01";

	// 身份证 0 护照 1 军官证 2 士兵证 3 户口本 5 其他 7
	/**
	 * 身份证 0
	 */
	public final String AERT_TYPE0 = "0";
	/**
	 * 护照 1
	 */
	public final String AERT_TYPE1 = "1";
	/**
	 * 军官证 2
	 */
	public final String AERT_TYPE2 = "2";
	/**
	 * 士兵证 3
	 */
	public final String AERT_TYPE3 = "3";
	/**
	 * 户口本 5
	 */
	public final String AERT_TYPE5 = "5";
	/**
	 * 其他 7
	 */
	public final String AERT_TYPE7 = "7";

	private final static String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
			"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8",
			"9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
			"U", "V", "W", "X", "Y", "Z" };

	public static String generateShortUuid() {
		StringBuffer shortBuffer = new StringBuffer();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		for (int i = 0; i < 8; i++) {
			String str = uuid.substring(i * 4, i * 4 + 4);
			int x = Integer.parseInt(str, 16);
			shortBuffer.append(chars[x % 0x3E]);
		}
		return shortBuffer.toString();

	}

	/**
	 * 判断手机号码是否有误
	 * 
	 * @param mobile
	 * @return
	 */
	public static boolean isMobile(String mobile) {
		return Pattern.matches(MOBILE_PATTERN, mobile);
	}

	/**
	 * 是否是数字
	 * 
	 * @param mobile
	 * @return
	 */
	public boolean isNumber(String number) {
		return Pattern.matches(NUMBER, number);
	}

	/**
	 * 判断是否是正确的IDNO(身份证号码)
	 *
	 * @param idno
	 * @return
	 */
	public static boolean isIDCard(String idno) {
		log.info(idno);
		return IDCard.isIdcard(idno);
	}

	/**
	 * 判断是否是正确的EMAIL(邮箱)
	 *
	 * @param idno
	 * @return
	 */
	public boolean isEmail(String email) {
		return Pattern.matches(EMAIL, email);
	}

	/**
	 * 数据检验
	 * 
	 * @param keyStr
	 * @param map
	 * @throws ErrorException
	 */
	@SuppressWarnings("rawtypes")
	public static void checkNeedDatas(String keyStr, Map map) throws ErrorException {
		String keys[] = keyStr.split("\\|");
		String value = "";
		for (String temp : keys) {
			try {
				value = map.get(temp).toString();
				log.info("[check key is:" + temp + "]");
			} catch (Exception e) {
				e.printStackTrace();
				throw new ErrorException("1001","请求参数错误");
			}
			if (StringUtils.isBlank(value)) {
				throw new ErrorException("1001","请求参数["+temp+"]为空");
			}
		}
	}

	/**
	 * 转换为bigdecimal
	 * 
	 * @param keyStr
	 * @param map
	 * @throws ErrorException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void toBigDecimal(String keyStr, Map map) throws ErrorException {
		String keys[] = keyStr.split("\\|");
		String value = "";
		for (String temp : keys) {
			try {
				value = (String) map.get(temp);
				map.put(temp, new BigDecimal(value).divide(new BigDecimal(100)).toPlainString());
			} catch (Exception e) {
				e.printStackTrace();
				throw new ErrorException("2003");
			}
		}
	}

	/**
	 * 获取6位数字验证码
	 * 
	 * @param nums=6
	 * @return
	 */
	public static String getCode(int nums) {
		StringBuffer str = new StringBuffer();
		for (int i = 0; i < nums; i++) {
			int random = (int) (Math.random() * 10);
			str.append(random);
		}
		return str.toString();
	}

	public static void main(String[] args) throws ErrorException {
		System.out.println(TransUtil.getTransId());
	}
}
